unit uOhmLawForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBaseCalcForm, StdCtrls, ExtCtrls;

type
  TfmOhmLaw = class(TfmBaseCalcForm)
    rgOption: TRadioGroup;
    edP1: TLabeledEdit;
    laP1: TLabel;
    laP2: TLabel;
    edP2: TLabeledEdit;
    Bevel1: TBevel;
    edResult: TLabeledEdit;
    laResult: TLabel;
    laFormula: TLabel;
    edResult1: TLabeledEdit;
    laResult1: TLabel;
    procedure rgOptionClick(Sender: TObject);
    procedure DoFieldChange(Sender: TObject);
    procedure DoFloatKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  protected
    function DoCheckFields: boolean; override;
    procedure FieldsChanged; override;
  public
    { Public declarations }
    class function CalcFriendlyName: string; override;
    class function RunKey: string; override;
    class procedure InvokeCalc; override;
    procedure AfterConstruction; override;
  end;

var
  fmOhmLaw: TfmOhmLaw;

implementation

{$R *.dfm}

uses uFormReg, uLocalization;

var
  sfAmp: array[0..1] of string;

{ TfmOhmLaw }

procedure TfmOhmLaw.AfterConstruction;
begin
  inherited;
  sfAmp[0] := getString('#global\amp');
  sfAmp[1] := getString('#global\ma');
  
  rgOption.Caption := getLocaleString('seltitle');
  rgOption.ItemIndex := 0;
end;

class function TfmOhmLaw.CalcFriendlyName: string;
begin
  Result := 'Power calculator';
end;

class procedure TfmOhmLaw.InvokeCalc;
begin
  Application.CreateForm(TfmOhmLaw, fmOhmLaw);
  inherited;
end;

class function TfmOhmLaw.RunKey: string;
begin
  Result := 'pwrcalc';
end;

procedure TfmOhmLaw.rgOptionClick(Sender: TObject);
begin
  inherited;
  Self.LockFields;
  try
    edP1.Text := '';
    edP2.Text := '';
    edResult.Text := '';
  finally
    Self.UnlockFields;
  end;
  edResult1.Visible := rgOption.ItemIndex in [1,3];
  laResult1.Visible := edResult1.Visible;
  laFormula.Caption := getString('#global\formula')+': ';

  case rgOption.ItemIndex of
    0: begin
         SetLabels('P1', getString('#global\vcc'), getString('#global\volt'));
         SetLabels('P2', getString('#global\current'), getString('#global\amp'));
         SetLabels('Result', getString('#global\power'), getString('#global\watt'));
         laFormula.Caption := laFormula.Caption+'P=V*I';
       end;
    1: begin
         SetLabels('P1', getString('#global\vcc'), getString('#global\volt'));
         SetLabels('P2', getString('#global\resistance'), getString('#global\ohm'));
         SetLabels('Result', getString('#global\current'), getString('#global\amp'));
         SetLabels('Result1', getString('#global\power'), getString('#global\watt'));
         laFormula.Caption := laFormula.Caption+'I=V/R, P=V*I';
       end;
    2: begin
         SetLabels('P1', getString('#global\current'), getString('#global\amp'));
         SetLabels('P2', getString('#global\resistance'), getString('#global\ohm'));
         SetLabels('Result', getString('#global\power'), getString('#global\watt'));
         laFormula.Caption := laFormula.Caption+'P=I^2*R';
       end;
    3: begin
         SetLabels('P2', getString('#global\current'), getString('#global\amp'));
         SetLabels('P1', getString('#global\resistance'), getString('#global\ohm'));
         SetLabels('Result', getLocaleString('voltdrop'), getString('#global\volt'));
         SetLabels('Result1', getString('#global\pwrdiss'), getString('#global\watt'));
         laFormula.Caption := laFormula.Caption+'V=I*R, P=I^2*R';
       end;
  end;
end;

function TfmOhmLaw.DoCheckFields: boolean;
begin
  Result := (checkField(edP1)>0) and (checkField(edP2)>0);
end;

procedure TfmOhmLaw.FieldsChanged;
var data: array[0..3] of single;
begin
  inherited;
  data[0] := checkField(edP1);
  data[1] := checkField(edP2);

  case rgOption.ItemIndex of
    0: data[2] := data[0]*data[1];
    1: begin
         data[2] := data[0]/data[1];
         data[3] := data[0]*data[2];
         if data[2]<0.01 then begin
           laResult.Caption := sfAmp[1];
           data[2] := data[2]*1e3;
         end else laResult.Caption := sfAmp[0];
       end;
    2: data[2] := sqr(data[0])*data[1];
    3: begin
         data[2] := data[0]*data[1];
         data[3] := sqr(data[1])*data[0];
       end;
  end;

  edResult.Text := format('%.6f', [data[2]]);
  if (rgOption.ItemIndex in [1,3]) then
    edResult1.Text := format('%.3f', [data[3]]);
end;

procedure TfmOhmLaw.DoFieldChange(Sender: TObject);
begin
  inherited;
  Self.DoFieldChanged;
end;

procedure TfmOhmLaw.DoFloatKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['-','0'..'9',chr(VK_BACK),chr(VK_DELETE),DecimalSeparator]) then Key := #0;
end;

initialization
  RegisterCalc(TfmOhmLaw);
  
end.
